// pages/editAddress/editAddress.js

var http = require("../../utils/http.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //地址对应id
    addrId: 0,
    name: "",
    detailAddress: "",
    mobile: "",
    //地址区号
    areaId: "110101",
    province: "北京市",
    city: "北京市",
    area: "东城区",
    isDefault: 0,
    ifEdit: false,
    userId: ""

  },

  onShow() {
    console.log("editAddress")
    const util = require('../../utils/util')
    util.isLogin()
    this.setData({
      userId: wx.getStorageSync('userId')
    })
    console.log(this.data.userId, wx.getStorageSync('userId'))

    //修改地址
    if (option.addrId) {
      this.setData({
        addrId: option.addrId,
        ifEdit: true
      });
      console.log('addrId', this.data.addrId);
      this.getInfo();
    }
    //添加地址-什么都不做
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(option) {
    console.log("editAddress")
    const util = require('../../utils/util')
    util.isLogin()
    this.setData({
      userId: wx.getStorageSync('userId')
    })
    console.log(this.data.userId, wx.getStorageSync('userId'))

    //修改地址
    if (option.addrId) {
      this.setData({
        addrId: option.addrId,
        ifEdit: true
      });
      console.log('addrId', this.data.addrId);
      this.getInfo();
    }
    //添加地址-什么都不做
  },

  /***
   * 判断是否为修改地址，是则获取信息
   */
  getInfo() {
    var ths = this;
    var addrId = this.data.addrId;
    wx.showLoading();
    var params = {
      url: "/address",
      method: "GET",
      data: {
        userId: this.data.userId,
        id: addrId
      },
      callBack: function (res) {
        console.log(res);
        ths.setData({
          addrId: res[0].id,
          name: res[0].name,
          detailAddress: res[0].detailAddress,
          mobile: res[0].mobile,
          areaId: res[0].areaId,
          province: res[0].province,
          city: res[0].city,
          area: res[0].area,
          isDefault: res[0].isDefault
        });
        wx.hideLoading();
      }
    }
    http.request(params);


  },

  /**
   * 提交添加地址/编辑地址
   */
  submit: function () {
    var name = this.data.name;
    var detailAddress = this.data.detailAddress;
    var mobile = this.data.mobile;
    var areaId = this.data.areaId;
    var province = this.data.province;
    var city = this.data.city;
    var area = this.data.area;
    var addrId = this.data.addrId;
    var userId = this.data.userId;

    console.log(userId == '', name == '', detailAddress == '', mobile == '', areaId == '')
    if (userId == '' || name == '' || detailAddress == '' || mobile == '' || areaId == '') {
      wx.showToast({
        title: '请输入完整信息',
      })
      return false
    } else {
      wx.showLoading();
      if (!this.data.ifEdit) {
        //添加地址
        var params = {
          url: "/address",
          method: "POST",
          data: {
            name: name,
            userId: userId,
            detailAddress: detailAddress,
            mobile: mobile,
            areaId: areaId,
          },
          callBack: function (res) {
            console.log("add", res);
            wx.navigateBack({ //返回上一页
              delta: 1
            })
            wx.hideLoading();
          }
        }

      } else {
        //编辑地址
        var params = {
          url: "/address",
          method: "PUT",
          data: {
            id: addrId,
            userId: userId,
            name: name,
            detailAddress: detailAddress,
            mobile: mobile,
            areaId: this.data.areaId,
            province: province,
            city: city,
            area: area,
          },
          callBack: function (res) {
            console.log(res);
            wx.navigateBack({ //返回上一页
              delta: 1
            })
            wx.hideLoading();
          }
        }
      }
      http.request(params);
    }

  },

  /**
   * 获取地址信息
   */
  bindRegionChange: function (e) {
    this.setData({
      province: e.detail.value[0],
      city: e.detail.value[1],
      area: e.detail.value[2],
      areaId: e.detail.code[2]
    })
  },
  receiverInput(e) {
    if (e.detail.value) {
      this.setData({
        name: e.detail.value
      });
    }
  },
  mobileInput(e) {
    if (e.detail.value) {
      this.setData({
        mobile: e.detail.value
      });
    }
  },
  detailedAddressInput(e) {
    if (e.detail.value) {
      this.setData({
        detailAddress: e.detail.value
      });
    }
  },
  switchChange(e) {
    let isDefault = (e.detail.value == 1)
    if (e.detail.value) {
      this.setData({
        isDefault: isDefault
      });
    }
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})